<template>
    <div class="app-container">
      <!--工具栏-->
      <Search :crud="crud">
        <!-- <el-form-item label="主题">
            <el-input v-model="query.theme" placeholder="请输入"></el-input>
        </el-form-item> -->
        <el-form-item label="状态">
          <el-select v-model="query.isRelease" placeholder="请选择">
            <el-option
              v-for="item in dict.COMMON_STATUS_DICT"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </Search>
      <el-card>
        <div class="head-container">
            <crudOperation :permission="permission">
                <template slot="right">
                    <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="handle(null,'new')">新建</el-button>
                    <el-button class="filter-item" size="mini" type="primary" icon="el-icon-delete" @click="handle(null,'batchDelete')">批量删除</el-button>
                </template>
            </crudOperation>
        </div>
        <el-table ref="table" border v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
            <el-table-column type="selection" width="55" align="center" fixed="left"/>
            <el-table-column label="序号" type="index" width="50" align="center"/>
            <el-table-column prop="url" label="品牌图片" align="center" width="120">
              <template slot-scope="scope" >
                <template v-if="scope.row.tbDemoMoldTakingFileList != undefined && scope.row.tbDemoMoldTakingFileList.length>0">
                  <el-badge :value="scope.row.tbDemoMoldTakingFileList.length" class="item">
                    <el-image
                  style="width: 80px; height: 80px"
                  :src="scope.row.tbDemoMoldTakingFileList[0].fileUrl"
                  fit="fill" @click="handle(scope.row,'checkImg')"></el-image>
                  </el-badge>
                </template>
              </template>
            </el-table-column>
            <el-table-column prop="theme" label="品牌主题" align="center"/>
            <el-table-column prop="createdName" label="创建人" align="center"/>
            <el-table-column prop="updatedAt" label="创建日期" width="200" align="center"/>
            <el-table-column prop="status" label="状态" align="center" fixed="right">
              <template slot-scope="scope" >
                  <el-switch v-model="scope.row.isRelease" active-value="1" inactive-value="0" @change="changeEnabled(scope.row, scope.row.isRelease)"></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="200"  fixed="right">
              <template slot-scope="scope" >
                  <el-button type="danger" size="mini" @click="handle(scope.row,'delete')">删除</el-button>
                  <el-button type="primary" size="mini" @click="handle(scope.row,'update')">修改</el-button>
              </template>
            </el-table-column>
        </el-table>

        <!--分页组件-->
        <pagination />
      </el-card>

      <BrandIntroductionDialog ref="brandIntroductionDialog" @submitSuccess="submitSuccess"></BrandIntroductionDialog>
    </div>
  </template>
  
  <script>
  import crudJob from '@/api/system/job'
  import CRUD, { presenter, header, crud } from '@crud/crud'
  import crudOperation from '@crud/CRUD.operation'
  import pagination from '@crud/Pagination'
  import udOperation from '@crud/UD.operation'
  import Search from '@/components/Search'
import BrandIntroductionDialog from './brandIntroductionDialog.vue'
import product from '@/api/product/product'
  export default {
    name: 'BrandIntroductionManage',
    components: { crudOperation, pagination, udOperation, Search, BrandIntroductionDialog },
    cruds() {
      return CRUD({
        title: '品牌介绍',
        url: 'tbDemoMoldTaking/getCostSubjectList',
        requestMethod: 'post',
        sort: ['jobSort,asc2', 'id,desc'],
        // takingType:'3',
        otherParams:{'takingType':'3'},
        crudMethod: { ...crudJob }
      })
    },
    // mixins: [presenter()],
    mixins: [presenter(), header(), crud()],
    // 数据字典
    dicts: ['COMMON_STATUS_DICT'],
    data() {
      return {
        permission: {
          add: ['admin', 'job:add'],
          edit: ['admin', 'job:edit'],
          del: ['admin', 'job:del']
        },
        dataList:[
          {'id':1,'name' : '西瓜', 'price':100,'discountPrice':80,'saleNum':300, 'url' : 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F20182%2F7%2F2018271371_JQrBv.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1699068397&t=c81e2b357c9dbee1442ed26c7b8c5c19', 'code' : '20231005112400001','createName' : '西瓜','createTime':'2023-10-05 11:30'},
          {'id':2,'name' : '西瓜', 'price':100,'discountPrice':80,'saleNum':300,  'url' : 'https://img2.baidu.com/it/u=310868834,4209286231&fm=253&fmt=auto?w=400&h=400', 'code' : '20231005112400001','createName' : '西瓜','createTime':'2023-10-05 11:30'},
          {'id':3,'name' : '西瓜', 'price':100,'discountPrice':80,'saleNum':300,  'url' : 'https://img.zcool.cn/community/01cc385f9b63a611013ee04d5c8f7c.jpg@2o.jpg', 'code' : '20231005112400001','createName' : '西瓜','createTime':'2023-10-05 11:30'},
          {'id':4,'name' : '西瓜', 'price':100,'discountPrice':80,'saleNum':300,  'url' : 'https://img2.woyaogexing.com/2020/04/20/5d3b91cea1b34f768e1495485d1a32d8%21400x400.jpeg', 'code' : '20231005112400001','createName' : '西瓜','createTime':'2023-10-05 11:30'},
          {'id':5,'name' : '西瓜', 'price':100,'discountPrice':80,'saleNum':300,  'url' : 'https://img2.woyaogexing.com/2019/06/20/4dab63a977e84d3b9564d28e52348f02%21400x400.jpeg', 'code' : '20231005112400001','createName' : '西瓜','createTime':'2023-10-05 11:30'},
          // {'id':6,'name' : '西瓜', 'price':100,'discountPrice':80,'saleNum':300,  'url' : 'https://img0.baidu.com/it/u=2933708314,2549584673&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400', 'code' : '20231005112400001','createName' : '西瓜','createTime':'2023-10-05 11:30'},
        ], //crud.data
      }
    },
    methods: {
      // 改变状态
      changeEnabled(data, val) {
        product.tbDemoMoldTakingUpdateStatus({id:data.id,isRelease:val}).then(() => {
          this.$notify({ message: '操作成功', type: 'success'});
          this.crud.refresh();
        }).catch(err => {
          data.isRelease = !data.isRelease
          if (data.isRelease=='1') {
            data.isRelease = '0';
          }else{
            data.isRelease = '1';
          }
        })
      },
      // 删除与修改事件
      handle(row,type){
        if (type == 'delete') {//删除
          this.deleteHandle([row.id]);
        }else if (type == 'update') {// 修改
          let forms = JSON.parse(JSON.stringify(row));
          for (let index = 0; index < forms.tbDemoMoldTakingFileList.length; index++) {
            const element = forms.tbDemoMoldTakingFileList[index];
            element.name = element.fileName;
            element.url = element.fileUrl;
          }
          for (let index = 0; index < forms.tbDemoMoldTakingIsVideoFileList.length; index++) {
            const element = forms.tbDemoMoldTakingIsVideoFileList[index];
            element.name = element.fileName;
            element.url = element.fileUrl;
          }
          console.log(JSON.stringify(forms.tbDemoMoldTakingFileList));
          this.$refs.brandIntroductionDialog.form = {//forms
            id: forms.id,
            theme: forms.theme,
            isRelease: forms.isRelease, // 发布标志，0-未发布，1-发布
            remark: forms.remark,//描述
            fileList: forms.tbDemoMoldTakingFileList,//封面
            videoList: forms.tbDemoMoldTakingIsVideoFileList,// 视频
          };
          // this.$refs.brandIntroductionDialog.form.fileList = forms.tbDemoMoldTakingFileList;
          // this.$refs.brandIntroductionDialog.form.videoList = forms.tbDemoMoldTakingIsVideoFileList;
          // console.log(JSON.stringify(this.$refs.brandIntroductionDialog.form));
          this.$refs.brandIntroductionDialog.isEdit = true;
          this.$refs.brandIntroductionDialog.dialogVisible = true;
        }else if (type == 'new') {// 新增
          this.$refs.brandIntroductionDialog.form = {
            theme: '',
            isRelease: '', // 发布标志，0-未发布，1-发布
            remark: '',//描述
            fileList:[],//封面
            videoList:[],// 视频
          }
          this.$refs.brandIntroductionDialog.isEdit = false;
          this.$refs.brandIntroductionDialog.dialogVisible = true;
        }else if (type == 'batchDelete') {// 批量删除
          if (this.crud.selections.length==0) {
            this.$notify({ title: '请选择需要删除的数据', type: 'warning' });
            return;
          }
          const idList = [];
          for (let index = 0; index < this.crud.selections.length; index++) {
            const element = this.crud.selections[index];
            idList.push(element.id);
          }
          console.log(JSON.stringify(idList));
          this.deleteHandle(idList);
        }else if (type == 'checkImg') {//查看图片
          let urlList = [];
          for (let index = 0; index < row.tbDemoMoldTakingFileList.length; index++) {
            const element = row.tbDemoMoldTakingFileList[index];
            urlList.push(element.fileUrl);
            
          }
          this.$viewerApi({ images: urlList })
        } else {
          
        }
      },
      // 删除
      deleteHandle(params){
        this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          product.tbDemoMoldTakingDelete(params).then((res) => {
            this.$notify({ message: '操作成功', type: 'success'});
            this.crud.refresh()
          }).catch(() => {})
        })
      },
      // 新建取模成功
      submitSuccess(){
        this.crud.refresh();
      }
    }
  }
  </script>
  
  <style rel="stylesheet/scss" lang="scss" scoped>
   ::v-deep .el-input-number .el-input__inner {
      text-align: left;
    }
    .item {
      margin-top: 10px;
      margin-right: 10px;
    }
  </style>
  